#ifndef LIBRARYCLIENT_H
#define LIBRARYCLIENT_H

#include <QRunnable>
#include <QDebug>
#include "library.h"

template <char prefix>
class LibraryClient : public QRunnable
{
    static size_t N;
    volatile bool _stop;
    volatile bool _stoped;
protected:
    const size_t _id;
public:
    LibraryClient() :
        _stop(false),
        _stoped(false),
        _id(N++)
    {
        setAutoDelete(true);
    }

    virtual ~LibraryClient(){}

    virtual void run()
    {
        _stop = _stoped = false;

        qDebug() << prefix << _id << "started";

        while(!_stop) {
            runOnce();
        }

        _stoped = true;
        qDebug() << prefix << _id << "stoped";
    }

    void stop()
    {
        _stop = true;
    }

    virtual void runOnce() = 0;
};

#endif // LIBRARYCLIENT_H
